#ifndef _PLAYER_C
#define _PLAYER_C

#include "player.h"

//Functions

void init_player(int amount){
    int i,j,k,ok=0,check=0;
    if(amount>MAX_PLAYER)amount=MAX_PLAYER;
      for(i=0;i<amount;i++){
        while(!ok){
         local[i].cur_x=rand()%590;
         local[i].cur_y=rand()%350;
         local[i].x1=local[i].cur_x+19;
         local[i].x2=local[i].cur_x+31;
         local[i].y1=local[i].cur_y+22;
         local[i].y2=local[i].cur_y+53;
         for(j=0;j<25;j++){
              for(k=0;k<40;k++){
                if(tiles[j][k].att!=0){
                 if(tile_col_player(tiles[j][k],local[i])){
                     check--;break;
                 }
                }        
              }
         }
         check++;
         for(j=0;j<MAX_ENEMY;j++){
               if(enemy[j].alive){
                   if(enemy_col_player(enemy[j],local[i])){
                       check--;break;
                   }
               }
         }  
         check++;
         if(check==2)ok=1;
         else check=0;
        }   
        local[i].id=i; 
        local[i].spr=PLAYER_STAND;
        local[i].dead=0;
        if(game_mode)local[i].energy=player_energy;
        else local[i].energy=1;
        local[i].hit=0;
        if(local[i].cur_x<320)local[i].dir=RIGHT;
        else local[i].dir=LEFT;
        local[i].mov=0;
        local[i].wait=0;
        local[i].shoot=0;
        local[i].shoot_dir=SHOOT_STRAIGHT;
        local[i].shoot_count=0;
      } 
      stop_sample(game_sound[P_ELEC].dat);
}       

void draw_player(int amount){
  int i;
  if(amount>MAX_PLAYER)amount=MAX_PLAYER;
  for(i=0;i<amount;i++){
    switch (local[i].dir){
        case LEFT:
        draw_sprite_h_flip(buffer,(BITMAP *) player_sprites[local[i].spr].dat,local[i].cur_x, local[i].cur_y);
        break;
        case RIGHT:
        draw_sprite(buffer,(BITMAP *) player_sprites[local[i].spr].dat, local[i].cur_x, local[i].cur_y);
        break;
    }
    if(show_rects)rect(buffer,local[i].x1,local[i].y1,local[i].x2,local[i].y2,makecol(255,255,255));
  }          
}

void player_control(int amount){
 int i,j;
 int p1=0; //temporary until I get a fix for the controls
 if(amount>MAX_PLAYER)amount=MAX_PLAYER;
 //for(i=0;i<amount;i++){
 //Need to find a better way to handle the controls for assignment of other players which is unlikely
     if(key[KEY_LEFT]&&!key[KEY_RIGHT]){
         if(!local[p1].shoot){
           local[p1].cur_x--;local[p1].x1--;local[p1].x2--;
           if(key[KEY_UP]||key[KEY_DOWN]);
           else local[p1].mov++;
         }    
         local[p1].dir=LEFT;
         local[p1].shoot_dir=SHOOT_STRAIGHT;
         if(key[KEY_UP])local[p1].shoot_dir=SHOOT_UP_CORNER_LEFT;
         if(key[KEY_DOWN])local[p1].shoot_dir=SHOOT_DOWN_CORNER_LEFT;
     }
     if(key[KEY_RIGHT]&&!key[KEY_LEFT]){
         if(!local[p1].shoot){
           local[p1].cur_x++;local[p1].x1++;local[p1].x2++;
           if(key[KEY_UP]||key[KEY_DOWN]);
           else local[p1].mov++;
         }    
         local[p1].dir=RIGHT;
         local[p1].shoot_dir=SHOOT_STRAIGHT;
         if(key[KEY_UP])local[p1].shoot_dir=SHOOT_UP_CORNER_RIGHT;
         if(key[KEY_DOWN])local[p1].shoot_dir=SHOOT_DOWN_CORNER_RIGHT;
     }    
     if(key[KEY_UP]&&!key[KEY_DOWN]){
         if(!local[p1].shoot){
           local[p1].cur_y--;local[p1].y1--;local[p1].y2--;
           if(!key[KEY_RIGHT]||!key[KEY_LEFT])local[p1].mov++;
         }    
         local[p1].shoot_dir=SHOOT_UP;
         if(key[KEY_RIGHT])local[p1].shoot_dir=SHOOT_UP_CORNER_RIGHT;
         if(key[KEY_LEFT])local[p1].shoot_dir=SHOOT_UP_CORNER_LEFT;
     }
     if(key[KEY_DOWN]&&!key[KEY_UP]){
         if(!local[p1].shoot){
           local[p1].cur_y++;local[p1].y1++;local[p1].y2++;
           if(!key[KEY_RIGHT]||!key[KEY_LEFT])local[p1].mov++;
         }    
         local[p1].shoot_dir=SHOOT_DOWN;
         if(key[KEY_RIGHT])local[p1].shoot_dir=SHOOT_DOWN_CORNER_RIGHT;
         if(key[KEY_LEFT])local[p1].shoot_dir=SHOOT_DOWN_CORNER_LEFT;
     }
     if(key[KEY_LCONTROL]){
         keyhold[KEY_LCONTROL]++;
         local[p1].shoot=1;local[p1].shoot_count++;
         switch(local[p1].shoot_dir){
             case SHOOT_STRAIGHT:
                 if(local[p1].shoot_count<10)local[p1].spr=PLAYER_SHOOT0;
                 else local[p1].spr=PLAYER_SHOOT1;
                 break;
             case SHOOT_UP_CORNER_LEFT:
                 if(local[p1].shoot_count<10)local[p1].spr=PLAYER_SHOOT_CORNER0;
                 else local[p1].spr=PLAYER_SHOOT_CORNER1;
                 break;
             case SHOOT_UP_CORNER_RIGHT:
                 if(local[p1].shoot_count<10)local[p1].spr=PLAYER_SHOOT_CORNER0;
                 else local[p1].spr=PLAYER_SHOOT_CORNER1;
                 break;
             case SHOOT_UP:
                 if(local[p1].shoot_count<10)local[p1].spr=PLAYER_SHOOT_UP0;
                 else local[p1].spr=PLAYER_SHOOT_UP1;
                 break;
             case SHOOT_DOWN_CORNER_LEFT:
                 if(local[p1].shoot_count<10)local[p1].spr=PLAYER_SHOOT_DOWN0;
                 else local[p1].spr=PLAYER_SHOOT_DOWN1;
                 break;
             case SHOOT_DOWN_CORNER_RIGHT:
                 if(local[p1].shoot_count<10)local[p1].spr=PLAYER_SHOOT_DOWN0;
                 else local[p1].spr=PLAYER_SHOOT_DOWN1;
                 break;
             case SHOOT_DOWN:
                 if(local[p1].shoot_count<10)local[p1].spr=PLAYER_SHOOT_DOWN0;
                 else local[p1].spr=PLAYER_SHOOT_DOWN1;
                 break;
         }        
         if(!local_shot[0].alive&&keyhold[KEY_LCONTROL]<=100){
             player_shoot(local[p1].id);
         }    
     }     
     else {
         if(!key[KEY_LCONTROL]){
             keyhold[KEY_LCONTROL]=0;
         }
     }     
     if(!key[KEY_DOWN]&&!key[KEY_UP]&&!key[KEY_LEFT]&&!key[KEY_RIGHT]){   
          if(!local[p1].shoot&&!local[p1].hit){
            local[p1].wait++;
            local[p1].mov=0; 
            if(local[p1].wait>0&&local[p1].wait<100)local[p1].spr=PLAYER_STAND;
            if(local[p1].wait>100&&local[p1].wait<105)local[p1].spr=PLAYER_BLINK0;
            if(local[p1].wait>105&&local[p1].wait<110)local[p1].spr=PLAYER_BLINK1;
            if(local[p1].wait>110&&local[p1].wait<115)local[p1].spr=PLAYER_BLINK2;
            if(local[p1].wait>115&&local[p1].wait<120)local[p1].spr=PLAYER_BLINK1;
            if(local[p1].wait>120&&local[p1].wait<125)local[p1].spr=PLAYER_BLINK0;
            if(local[p1].wait>125&&local[p1].wait<325)local[p1].spr=PLAYER_STAND;
            if(local[p1].wait>325&&local[p1].wait<330)local[p1].spr=PLAYER_BLINK0;
            if(local[p1].wait>330&&local[p1].wait<335)local[p1].spr=PLAYER_BLINK1;
            if(local[p1].wait>335&&local[p1].wait<340)local[p1].spr=PLAYER_BLINK2;
            if(local[p1].wait>340&&local[p1].wait<345)local[p1].spr=PLAYER_BLINK1;
            if(local[p1].wait>345&&local[p1].wait<350)local[p1].spr=PLAYER_BLINK0;
            if(local[p1].wait>350&&local[p1].wait<355)local[p1].spr=PLAYER_BLINK1;
            if(local[p1].wait>355&&local[p1].wait<360)local[p1].spr=PLAYER_BLINK2;
            if(local[p1].wait>360&&local[p1].wait<365)local[p1].spr=PLAYER_BLINK1;
            if(local[p1].wait>365&&local[p1].wait<370)local[p1].spr=PLAYER_BLINK0;
            if(local[p1].wait>370)local[p1].wait=0;
          }
          if(local[p1].hit){
              local[p1].mov++;
              if(local[p1].mov==0&&local[p1].mov<5)local[p1].spr=PLAYER_DIE1;
              if(local[p1].mov==5&&local[p1].mov<10)local[p1].spr=PLAYER_DIE2;
              if(local[p1].mov==10&&local[p1].mov<15)local[p1].spr=PLAYER_DIE1;
              if(local[p1].mov>15){local[p1].spr=PLAYER_STAND;local[p1].mov=0;local[p1].hit=0;}
          }    
     }
     
     if(!key[KEY_LCONTROL]||!key[KEY_SPACE]){
         local[p1].shoot=0;
         local[p1].shoot_dir=SHOOT_STRAIGHT;
         local[p1].shoot_count=0;
     }   
     
     if(local[p1].mov>0&&local[p1].mov<5&&!local[p1].shoot)local[p1].spr=PLAYER_RUN0;
     if(local[p1].mov==5&&local[p1].mov<10&&!local[p1].shoot)local[p1].spr=PLAYER_RUN1;
     if(local[p1].mov==10&&local[p1].mov<15&&!local[p1].shoot)local[p1].spr=PLAYER_RUN2;
     if(local[p1].mov==15&&local[p1].mov<20&&!local[p1].shoot)local[p1].spr=PLAYER_RUN3;
     if(local[p1].mov==20&&local[p1].mov<25&&!local[p1].shoot)local[p1].spr=PLAYER_RUN4;
     if(local[p1].mov==25&&local[p1].mov<30&&!local[p1].shoot)local[p1].spr=PLAYER_RUN5;
     if(local[p1].mov==30&&local[p1].mov<35&&!local[p1].shoot)local[p1].spr=PLAYER_RUN6;
     if(local[p1].mov==35&&local[p1].mov<40&&!local[p1].shoot)local[p1].spr=PLAYER_RUN7;
     if(local[p1].mov==40&&local[p1].mov<45&&!local[p1].shoot)local[p1].spr=PLAYER_RUN8;
     if(local[p1].mov==45&&local[p1].mov<50&&!local[p1].shoot)local[p1].spr=PLAYER_RUN9;
     if(local[p1].mov==50&&local[p1].mov<55&&!local[p1].shoot)local[p1].spr=PLAYER_RUN10;
     if(local[p1].mov==55&&local[p1].mov<60&&!local[p1].shoot)local[p1].spr=PLAYER_RUN11;
     if(local[p1].mov==60&&local[p1].mov<65&&!local[p1].shoot)local[p1].spr=PLAYER_RUN12;
     if(local[p1].mov==65&&local[p1].mov<70&&!local[p1].shoot)local[p1].spr=PLAYER_RUN13;
     if(local[p1].mov==70&&local[p1].mov<75&&!local[p1].shoot)local[p1].spr=PLAYER_RUN14;
     if(local[p1].mov==75&&local[p1].mov<80&&!local[p1].shoot)local[p1].spr=PLAYER_RUN15;
     if(local[p1].mov>80&&!local[p1].shoot){local[p1].spr=PLAYER_RUN2;local[p1].mov=10;}
     
     if(local[p1].hit){
              local[p1].mov++;
              if(local[p1].mov==0&&local[p1].mov<5)local[p1].spr=PLAYER_DIE1;
              if(local[p1].mov==5&&local[p1].mov<10)local[p1].spr=PLAYER_DIE2;
              if(local[p1].mov==10&&local[p1].mov<15)local[p1].spr=PLAYER_DIE1;
              if(local[p1].mov>15){local[p1].spr=PLAYER_STAND;local[p1].mov=0;local[p1].hit=0;}
     }
         
     for(i=0;i<25;i++){
         for(j=0;j<40;j++){
           if(tiles[i][j].att!=0){
            if(tile_col_player(tiles[i][j],local[p1])){
             local[p1].energy-=2;
             if(local[p1].energy>=1){
                 local[p1].hit=1;
                 play_sample(game_sound[P_ELEC].dat,sound_vol,128,1000,0);break;
             }
             if(local[p1].energy<=0){
                 local[p1].dead=1;local[p1].mov=0;
                 play_sample(game_sound[P_ELEC].dat,sound_vol,128,1000,1);break;
             }    
            }    
           }    
         }
     }
     for(j=0;j<MAX_ENEMY;j++){
             if(enemy[j].alive){
                 if(enemy_col_player(enemy[j],local[p1])){
                     local[p1].energy--;
                     if(local[p1].energy>=1){
                       local[p1].hit=1;
                       play_sample(game_sound[P_ELEC].dat,sound_vol,128,1000,0);break;
                     }
                     if(local[p1].energy<=0){
                       local[p1].dead=1;local[p1].mov=0;
                       play_sample(game_sound[P_ELEC].dat,sound_vol,128,1000,1);break;
                     }
                 }
             }
     }
     for(j=0;j<MAX_BALLS;j++){
             if(ball[j].alive){
                 if(enemy_col_player(ball[j],local[p1])){
                     local[p1].energy--;
                     if(local[p1].energy>=1){
                       local[p1].hit=1;
                       play_sample(game_sound[P_ELEC].dat,sound_vol,128,1000,0);break;
                     }
                     if(local[p1].energy<=0){
                       local[p1].dead=1;local[p1].mov=0;
                       play_sample(game_sound[P_ELEC].dat,sound_vol,128,1000,1);break;
                     }
                 }
             }
     }
     for(j=0;j<MAX_ENEMY;j++){
             if(enemy_shot[j].alive){
                 if(enemy_shot_col_player(enemy_shot[j],local[p1])){
                     local[p1].energy-=enemy_attack;kill_enemy_shot(enemy_shot[j].id);
                     if(local[p1].energy>=1){
                       local[p1].hit=1;
                       play_sample(game_sound[P_ELEC].dat,sound_vol,128,1000,0);break;
                     }
                     if(local[p1].energy<=0){
                       local[p1].dead=1;local[p1].mov=0;
                       play_sample(game_sound[P_ELEC].dat,sound_vol,128,1000,1);break;
                     }
                 }
             }
     }
}    

void player_death(int id){
     local[id].mov++;
     if(local[id].mov==0&&local[id].mov<5)local[id].spr=PLAYER_DIE0;
     if(local[id].mov==5&&local[id].mov<10)local[id].spr=PLAYER_DIE1;
     if(local[id].mov==10&&local[id].mov<15)local[id].spr=PLAYER_DIE2;
     if(local[id].mov==15&&local[id].mov<20)local[id].spr=PLAYER_DIE3;
     if(local[id].mov==20&&local[id].mov<25)local[id].spr=PLAYER_DIE4;
     if(local[id].mov==25&&local[id].mov<30)local[id].spr=PLAYER_DIE5;
     if(local[id].mov==30&&local[id].mov<35)local[id].spr=PLAYER_DIE6;
     if(local[id].mov==35&&local[id].mov<40)local[id].spr=PLAYER_DIE7;
     if(local[id].mov==40&&local[id].mov<45)local[id].spr=PLAYER_DIE8;
     if(local[id].mov==45&&local[id].mov<50)local[id].spr=PLAYER_DIE9;
     if(local[id].mov==50&&local[id].mov<55)local[id].spr=PLAYER_DIE10;
     if(local[id].mov>55){local[id].spr=PLAYER_DIE9;local[id].mov=45;}
}

void player_shoot(int id){
     local_shot[id].id=local[id].id;
     local_shot[id].alive=1;
     local_shot[id].dir=local[id].shoot_dir;
     local_shot[id].dir_face=local[id].dir;
     switch(local_shot[id].dir){
         case SHOOT_STRAIGHT:
           if(local[id].dir==RIGHT){
            local_shot[id].cur_x=local[id].cur_x+25;
            local_shot[id].cur_y=local[id].cur_y+2;
           }    
           else {
            local_shot[id].cur_x=local[id].cur_x-25;
            local_shot[id].cur_y=local[id].cur_y+2;
           }    
           local_shot[id].spr=PLAYER_SHOT_SIDE0;
           local_shot[id].x1=local_shot[id].cur_x+16;local_shot[id].x2=local_shot[id].cur_x+33;
           local_shot[id].y1=local_shot[id].cur_y+29;local_shot[id].y2=local_shot[id].cur_y+35;    
           break;
         case SHOOT_UP_CORNER_LEFT:
           local_shot[id].cur_x=local[id].cur_x-25;
           local_shot[id].cur_y=local[id].cur_y-10;
           local_shot[id].spr=PLAYER_SHOT_CORNER_LEFT0;
           local_shot[id].x1=local_shot[id].cur_x+16;local_shot[id].x2=local_shot[id].cur_x+21;
           local_shot[id].y1=local_shot[id].cur_y+23;local_shot[id].y2=local_shot[id].cur_y+28;       
           break;
         case SHOOT_UP_CORNER_RIGHT:
           local_shot[id].cur_x=local[id].cur_x+25;
           local_shot[id].cur_y=local[id].cur_y-12;
           local_shot[id].spr=PLAYER_SHOT_CORNER_RIGHT0;
           local_shot[id].x1=local_shot[id].cur_x+29;local_shot[id].x2=local_shot[id].cur_x+34;
           local_shot[id].y1=local_shot[id].cur_y+24;local_shot[id].y2=local_shot[id].cur_y+29;       
           break;
         case SHOOT_UP:
           if(local[id].dir==RIGHT){
            local_shot[id].cur_x=local[id].cur_x+2;
            local_shot[id].cur_y=local[id].cur_y-25;
           }    
           else {
            local_shot[id].cur_x=local[id].cur_x-2;
            local_shot[id].cur_y=local[id].cur_y-25;
           }
           local_shot[id].spr=PLAYER_SHOT_UP0;
           local_shot[id].x1=local_shot[id].cur_x+22;local_shot[id].x2=local_shot[id].cur_x+28;
           local_shot[id].y1=local_shot[id].cur_y+21;local_shot[id].y2=local_shot[id].cur_y+40;
            break;
         case SHOOT_DOWN_CORNER_LEFT:
           local_shot[id].cur_x=local[id].cur_x-25;
           local_shot[id].cur_y=local[id].cur_y+17;  
           local_shot[id].spr=PLAYER_SHOT_CORNER_RIGHT0;
           local_shot[id].x1=local_shot[id].cur_x+16;local_shot[id].x2=local_shot[id].cur_x+21;
           local_shot[id].y1=local_shot[id].cur_y+34;local_shot[id].y2=local_shot[id].cur_y+39;       
           break;
         case SHOOT_DOWN_CORNER_RIGHT:
           local_shot[id].cur_x=local[id].cur_x+25;
           local_shot[id].cur_y=local[id].cur_y+17;
           local_shot[id].spr=PLAYER_SHOT_CORNER_LEFT0;
           local_shot[id].x1=local_shot[id].cur_x+29;local_shot[id].x2=local_shot[id].cur_x+34;
           local_shot[id].y1=local_shot[id].cur_y+34;local_shot[id].y2=local_shot[id].cur_y+38;       
           break;
         case SHOOT_DOWN:
           if(local[id].dir==RIGHT){
            local_shot[id].cur_x=local[id].cur_x+20;
            local_shot[id].cur_y=local[id].cur_y+16;
           }    
           else {
            local_shot[id].cur_x=local[id].cur_x-20;
            local_shot[id].cur_y=local[id].cur_y+16;
           }
           local_shot[id].spr=PLAYER_SHOT_UP0;
           local_shot[id].x1=local_shot[id].cur_x+22;local_shot[id].x2=local_shot[id].cur_x+28;
           local_shot[id].y1=local_shot[id].cur_y+21;local_shot[id].y2=local_shot[id].cur_y+40;
           break;
     }        
     play_sample(game_sound[P_SHOT_SOUND].dat,sound_vol,128,1000,0);
}   

void kill_player_shot(int id){
    local_shot[id].cur_x=0;
    local_shot[id].cur_y=0;
    local_shot[id].x1=0;local_shot[id].x2=0;
    local_shot[id].y1=0;local_shot[id].y2=0;
    local_shot[id].spr=0;   
    local_shot[id].alive=0;
    local_shot[id].dir=0;
    local_shot[id].dir_face=0;
    stop_sample(game_sound[P_SHOT_SOUND].dat);
}    

void update_player_shot(int amount,int enemy_count){
   int i,j,k;
   if(amount>MAX_PLAYER)amount=MAX_PLAYER;
   for(i=0;i<amount;i++){
     if(local_shot[i].alive){
      switch(local_shot[i].dir){
        case SHOOT_STRAIGHT:
          if(local_shot[i].dir_face==LEFT){
             local_shot[i].cur_x-=3;local_shot[i].x1-=3;local_shot[i].x2-=3;
             if(local_shot[i].cur_x<-50){
                 kill_player_shot(local_shot[i].id);
             }    
          }
          else {
              local_shot[i].cur_x+=3;local_shot[i].x1+=3;local_shot[i].x2+=3;
              if(local_shot[i].cur_x>640){
                 kill_player_shot(local_shot[i].id);
              }    
          }        
          break;
        case SHOOT_UP_CORNER_LEFT:
          local_shot[i].cur_x-=3;local_shot[i].x1-=3;local_shot[i].x2-=3;
          local_shot[i].cur_y-=3;local_shot[i].y1-=3;local_shot[i].y2-=3;
          if(local_shot[i].cur_y<-50){
              kill_player_shot(local_shot[i].id);
          }    
          if(local_shot[i].cur_x<-50){
              kill_player_shot(local_shot[i].id);
          }    
          break;
        case SHOOT_UP_CORNER_RIGHT:
          local_shot[i].cur_x+=3;local_shot[i].x1+=3;local_shot[i].x2+=3;
          local_shot[i].cur_y-=3;local_shot[i].y1-=3;local_shot[i].y2-=3;
          if(local_shot[i].cur_y<-50){
             kill_player_shot(local_shot[i].id);
          }
          if(local_shot[i].cur_x>640){
              kill_player_shot(local_shot[i].id);
          }
          break;
        case SHOOT_UP:
          if(local_shot[i].dir_face==LEFT){
             local_shot[i].cur_y-=3;local_shot[i].y1-=3;local_shot[i].y2-=3;
             if(local_shot[i].cur_y<-50){
                 kill_player_shot(local_shot[i].id);
             }
          }
          else {
              local_shot[i].cur_y-=3;local_shot[i].y1-=3;local_shot[i].y2-=3;
              if(local_shot[i].cur_y<-50){
                 kill_player_shot(local_shot[i].id);
             }
          } 
          break;
        case SHOOT_DOWN_CORNER_LEFT:
          local_shot[i].cur_x-=3;local_shot[i].x1-=3;local_shot[i].x2-=3;
          local_shot[i].cur_y+=3;local_shot[i].y1+=3;local_shot[i].y2+=3;
          if(local_shot[i].cur_y>400){
              kill_player_shot(local_shot[i].id);
          }
          if(local_shot[i].cur_x<-50){
              kill_player_shot(local_shot[i].id);
          }    
          break;
        case SHOOT_DOWN_CORNER_RIGHT:
          local_shot[i].cur_x+=3;local_shot[i].x1+=3;local_shot[i].x2+=3;
          local_shot[i].cur_y+=3;local_shot[i].y1+=3;local_shot[i].y2+=3;
          if(local_shot[i].cur_y>400){
             kill_player_shot(local_shot[i].id);
          }
          if(local_shot[i].cur_x>640){
             kill_player_shot(local_shot[i].id);
          }
          break;
        case SHOOT_DOWN:
          if(local_shot[i].dir_face==LEFT){
             local_shot[i].cur_y+=3;local_shot[i].y1+=3;local_shot[i].y2+=3;
             if(local_shot[i].cur_y>400){
                 kill_player_shot(local_shot[i].id);
             }
          }
          else {
              local_shot[i].cur_y+=3;local_shot[i].y1+=3;local_shot[i].y2+=3;
              if(local_shot[i].cur_y>400){
                 kill_player_shot(local_shot[i].id);
             }
          } 
          break;
      }      
      for(j=0;j<25;j++){
         for(k=0;k<40;k++){
           if(tiles[j][k].att!=0){
            if(tile_col_shot(tiles[j][k],local_shot[i])){
             kill_player_shot(local_shot[i].id);break;
            }    
           }    
         }
      }
      for(j=0;j<enemy_count;j++){
             if(enemy[j].alive&&!enemy[j].dying){
                 if(enemy_col_player_shot(enemy[j],local_shot[i])){
                     kill_player_shot(local_shot[i].id);
                     enemy[j].energy-=player_attack;
                     show_enemy=enemy[j].id;
                     show_enemy_time=1;
                     if(enemy[j].energy>=1){
                         enemy[j].hit=1;
                         break;
                     }
                     if(enemy[j].energy<=0){  
                         kill_enemy(enemy[j].id);
                         //local[i].points+=50;
                         point_adder+=50;//give_points(50);
                         break;
                     }    
                 }
             }
     }
     for(j=0;j<enemy_count;j++){
             if(enemy_shot[j].alive){
                 if(enemy_shot_col_player_shot(enemy_shot[j],local_shot[i])){
                     kill_player_shot(local_shot[i].id);
                     kill_enemy_shot(enemy[j].id);break;
                 }
             }
     }
     for(j=0;j<MAX_BALLS;j++){
             if(ball[j].alive&&!ball[j].dying){
                 if(enemy_col_player_shot(ball[j],local_shot[i])){
                     kill_ball(ball[j].id);
                     //local[i].points+=20;
                     point_adder+=20;//give_points(20);
                     kill_player_shot(local_shot[i].id);
                     break;
                 }
             }
     }
    }    
   }    
}

void draw_player_shot(int amount){
  int i;
  if(amount>MAX_PLAYER)amount=MAX_PLAYER;
  for(i=0;i<amount;i++){
   if(local_shot[i].alive){
     draw_sprite(buffer, player_sprites[local_shot[i].spr].dat, local_shot[i].cur_x, local_shot[i].cur_y);
     if(show_rects)rect(buffer,local_shot[i].x1,local_shot[i].y1,local_shot[i].x2,local_shot[i].y2,makecol(255,255,255));
   }   
  }        
}   

//Player related collisions
int tile_col_player(struct TILE t,struct player p){
    if(t.x1 > p.x2) return 0;
    if(t.x2 < p.x1) return 0;
    if(t.y1 > p.y2) return 0;
    if(t.y2 < p.y1) return 0;
    return 1;
}    

int tile_col_shot(struct TILE t,struct  shot s){
    if(t.x1 > s.x2) return 0;
    if(t.x2 < s.x1) return 0;
    if(t.y1 > s.y2) return 0;
    if(t.y2 < s.y1) return 0;
    return 1;
}  

int enemy_col_player(struct enemy t,struct player p){
    if(t.x1 > p.x2) return 0;
    if(t.x2 < p.x1) return 0;
    if(t.y1 > p.y2) return 0;
    if(t.y2 < p.y1) return 0;
    return 1;
} 

int enemy_shot_col_player(struct enemy_shot s,struct player p){
    if(s.x1 > p.x2) return 0;
    if(s.x2 < p.x1) return 0;
    if(s.y1 > p.y2) return 0;
    if(s.y2 < p.y1) return 0;
    return 1;
} 

int enemy_col_player_shot(struct enemy e,struct shot s){
    if(e.x1 > s.x2) return 0;
    if(e.x2 < s.x1) return 0;
    if(e.y1 > s.y2) return 0;
    if(e.y2 < s.y1) return 0;
    return 1;
}          

int enemy_shot_col_player_shot(struct enemy_shot e,struct shot s){
    if(e.x1 > s.x2) return 0;
    if(e.x2 < s.x1) return 0;
    if(e.y1 > s.y2) return 0;
    if(e.y2 < s.y1) return 0;
    return 1;
}  

#endif
